package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSummaryService;
import com.bidevalution.dao.entity.BeSectionMethod;
import com.bidevalution.dao.entity.BeSummary;
import com.bidevalution.dao.mapper.BeSectionMethodMapper;
import com.bidevalution.dao.mapper.BeSummaryMapper;
import com.bidevalution.support.context.EvalutionMethodEnum;
import com.qtp.core.util.OConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 汇总信息
 * @Date        2020-03-25 14:55
 * @Author      lhl
 */
@Log4j2
@Service
public class BeSummaryServiceImpl implements BeSummaryService {

    @Autowired(required = false)
    private BeSummaryMapper beSummaryMapper;
    @Autowired(required = false)
    private BeSectionMethodMapper beSectionMethodMapper;


    @Override
    public Map<String, Object> getSummaryInfo(String sectionId,String sectionStepId) {
        List<Map<String,Object>> unitList = beSummaryMapper.getSummaryComments(sectionId,sectionStepId);
        List<Map<String, Object>> expertSummaryList = new ArrayList<>();
        List<Map<String,Object>> relList = new ArrayList<>();
        Map<String,Object> itemYs,itemBz;
        Map<String,Object> expertSummary;
        String enterpriseId;
        for (Map<String, Object> unitMap : unitList) {
            enterpriseId = OConvertUtils.getString(unitMap.get("enterpriseId"));
            //获取专家信息
            expertSummaryList = beSummaryMapper.getExpertSummaryInfo(enterpriseId, sectionStepId);
            /*放入优势信息*/
            itemYs = new HashMap<>();
            itemYs.put("tbdw",OConvertUtils.getString(unitMap.get("tbdw")));
            itemYs.put("enterpriseId",enterpriseId);
            itemYs.put("summaryId",OConvertUtils.getString(unitMap.get("summaryId")));
            itemYs.put("merit",OConvertUtils.getString(unitMap.get("merit")));
            itemYs.put("type","优势");
            /*放入不足信息*/
            itemBz = new HashMap<>();
            itemBz.putAll(itemYs);
            itemBz.put("merit",OConvertUtils.getString(unitMap.get("deficiency")));
            itemBz.put("type","不足");
            for (int i = 0; i < expertSummaryList.size(); i++) {
                expertSummary = expertSummaryList.get(i);
                itemYs.put("zj" + i,OConvertUtils.getString(expertSummary.get("ys")));
                itemBz.put("zj" + i,OConvertUtils.getString(expertSummary.get("bz")));
            }
            relList.add(itemYs);
            relList.add(itemBz);
        }
        /*设置header*/
        List<String> headerList = new ArrayList<>();
        headerList.add("序号");
        headerList.add("投标单位");
        headerList.add("");
        headerList.add("评标组长意见汇总");
        for (int i = 0; i < expertSummaryList.size(); i++) {
            expertSummary = expertSummaryList.get(i);
            headerList.add(OConvertUtils.getString(expertSummary.get("name")));
        }
        Map<String,Object> relMap = new HashMap<String,Object>();
        relMap.put("header",headerList);
        relMap.put("body",relList);
        relMap.put("experTotal",expertSummaryList.size());
        return relMap;
    }

    @Override
    public void updateSummaryInfo(BeSummary beSummary) {
        beSummaryMapper.updateSummaryInfo(beSummary);
    }

    @Override
    public void syncReviewOpinionInfo(String sectionId, String sectionStepId) {
        BeSectionMethod beSectionMethod = beSectionMethodMapper.selectByPrimaryKey(sectionId);
        if (EvalutionMethodEnum.SJPD.getCode().equals(beSectionMethod.getMethodCode())) {
            BeSummary beSummary = new BeSummary();
            beSummary.setSectionId(sectionId);
            beSummary.setDel(1);
            beSummaryMapper.updateSummaryInfo(beSummary);
            beSummaryMapper.insertReviewOpinionInfo(sectionId,sectionStepId);
        }
    }
}
